package cn.iocoder.yudao.module.eval.service.assignment;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.eval.controller.admin.assignment.vo.EvalPersonnelAssignmentPageReqVO;
import cn.iocoder.yudao.module.eval.dal.dataobject.assignment.EvalPersonnelAssignmentDO;
import cn.iocoder.yudao.module.eval.dal.mysql.assignment.EvalPersonnelAssignmentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 人员分配 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class EvalPersonnelAssignmentServiceImpl implements EvalPersonnelAssignmentService {

    @Resource
    private EvalPersonnelAssignmentMapper assignmentMapper;

    @Override
    public Long createAssignment(Long planId, Long userId, Integer personType, Long subjectId) {
        // 创建人员分配
        EvalPersonnelAssignmentDO assignment = new EvalPersonnelAssignmentDO();
        assignment.setPlanId(planId);
        assignment.setUserId(userId);
        assignment.setPersonType(personType);
        assignment.setSubjectId(subjectId);
        assignment.setAssignmentStatus(1); // 已分配
        assignment.setNotifyStatus(0); // 待通知
        assignment.setConfirmStatus(0); // 待确认
        assignmentMapper.insert(assignment);
        return assignment.getId();
    }

    @Override
    public void updateAssignment(Long id, Integer assignmentStatus) {
        // 校验存在
        validateAssignmentExists(id);
        // 更新
        EvalPersonnelAssignmentDO updateObj = new EvalPersonnelAssignmentDO();
        updateObj.setId(id);
        updateObj.setAssignmentStatus(assignmentStatus);
        assignmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteAssignment(Long id) {
        // 校验存在
        validateAssignmentExists(id);
        // 删除
        assignmentMapper.deleteById(id);
    }

    private void validateAssignmentExists(Long id) {
        if (assignmentMapper.selectById(id) == null) {
            throw new RuntimeException("人员分配不存在");
        }
    }

    @Override
    public EvalPersonnelAssignmentDO getAssignment(Long id) {
        return assignmentMapper.selectById(id);
    }

    @Override
    public PageResult<EvalPersonnelAssignmentDO> getAssignmentPage(EvalPersonnelAssignmentPageReqVO pageReqVO) {
        return assignmentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EvalPersonnelAssignmentDO> getAssignmentsByPlan(Long planId) {
        return assignmentMapper.selectListByPlanId(planId);
    }

    @Override
    public Boolean autoAssignPersonnel(Long planId) {
        // TODO: 实现自动分配逻辑
        log.info("自动分配人员，计划ID：{}", planId);
        return true;
    }

} 